package info.androidhive.pratap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;


import info.androidhive.volleyexamples.R;
import info.androidhive.volleyexamples.app.AppController;
import info.androidhive.volleyexamples.volley.utils.Const;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CustomListviewActivity extends Activity {

	private ProgressDialog pDialog;
	private String TAG = CustomListviewActivity.class.getSimpleName();

	private TextView msgResponse;

	private String[] sorucedataArray;
	private ListView dataListView;

	// These tags will be used to cancel the requests
	private String tag_json_obj = "jobj_req", tag_json_arry = "jarray_req";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.customlistview_activity);

		dataListView = (ListView) findViewById(R.id.dataListView);

		pDialog = new ProgressDialog(this);
		pDialog.setMessage("Loading...");
		pDialog.setCancelable(false);

		makeJsonArryReq();
	}

	
	/**
	 * Making json array request
	 * */
	private void makeJsonArryReq() {
		showProgressDialog();
		JsonArrayRequest req = new JsonArrayRequest(Const.ALL_SOURCE_LOCATIONS,
				new Response.Listener<JSONArray>() {
					@Override
					public void onResponse(JSONArray response) {
						Log.d(TAG, response.toString());

						parseJsonforListView(response);
						// msgResponse.setText(response.toString());
						hideProgressDialog();
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						VolleyLog.d(TAG, "Error: " + error.getMessage());
						hideProgressDialog();
					}
				});

		// Adding request to request queue
		AppController.getInstance().addToRequestQueue(req, tag_json_arry);

		// Cancelling request
		// ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_arry);
	}

	protected void parseJsonforListView(JSONArray response) {

		if (response != null) {
			try {

				// JSONObject jsonObj = new JSONObject(jsonStr);

				String data = null;

				// JSONArray jsa = new JSONArray(jsonStr);

				// Getting JSON Array node
				// jsonArrayAllSoruces = jsonObj.getJSONArray("");

				// looping through All Contacts
				for (int i = 0; i < response.length(); i++) {
					JSONObject c = response.getJSONObject(i);

					if (i >= 1) {

						data = data + "," + c.getString("tname");
					} else {
						data = c.getString("tname");
					}

					// adding contact to contact list
					// allSoruces.add(cityName);

				}
				sorucedataArray = data.split(",");

				ArrayAdapter<String> sourceAdapter = new ArrayAdapter<String>(
						CustomListviewActivity.this,
						android.R.layout.simple_list_item_1, sorucedataArray);

				dataListView.setAdapter(sourceAdapter);

			} catch (JSONException e) {
				e.printStackTrace();
			}
		} else {
			Log.e("ServiceHandler", "Couldn't get any data from the url");
		}

	}
	
	
	private void showProgressDialog() {
		if (!pDialog.isShowing())
			pDialog.show();
	}

	private void hideProgressDialog() {
		if (pDialog.isShowing())
			pDialog.hide();
	}

	private void dismissProgressDialog() {
		if (pDialog != null)
			pDialog.dismiss();

	}


	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		dismissProgressDialog();

	}

}
